<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增采购单')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-purchase-add">
        <h4 class="form-header h4">采购单信息</h4>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">[[#{warehouse}]]：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="warehouseId" class="form-control" type="hidden">
                        <input id="suggest-warehouse" class="form-control" type="text" required>
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right" role="menu">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{supplier}]]：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="supplierId" class="form-control" type="hidden">
                        <input id="suggest-supplier" class="form-control" type="text">
                        <div class="input-group-btn">
                            <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right" role="menu">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">[[#{purchaseOrderNumber}]]：</label>
                <div class="col-sm-8">
                    <input name="orderNo" th:value="${purchaseNo}" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{attributes}]]：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('jt_product_attributes')}">
                        <input type="radio" th:id="${'attributes_' + dict.dictCode}" name="attributes" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'attributes_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{logisticsCompany}]]：</label>
                <div class="col-sm-8">
                    <input name="logisticsCompany" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{logisticsOrderNumber}]]：</label>
                <div class="col-sm-8">
                    <input name="logisticsNumber" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{purchaser}]]：</label>
                <div class="col-sm-8">
                    <input name="purchaser" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">[[#{purchaseDate}]]：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="purchaseDate" class="form-control" th:placeholder="#{purchaseDate}" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{expectedDateOfArrival}]]：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="expectedArrivalDate" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{receiver}]]：</label>
                <div class="col-sm-8">
                    <input name="receiver" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{phoneNumber}]]：</label>
                <div class="col-sm-8">
                    <input name="phoneNumber" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{region}]]：</label>
                <div class="col-sm-8">
                    <input name="country" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{province}]]：</label>
                <div class="col-sm-8">
                    <input name="province" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{city}]]：</label>
                <div class="col-sm-8">
                    <input name="city" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{district}]]：</label>
                <div class="col-sm-8">
                    <input name="district" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{detailedAddress}]]：</label>
                <div class="col-sm-8">
                    <input name="fullAddress" class="form-control" type="text">
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label">[[#{remark}]]：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" type="text">
                </div>
            </div>
        </div>
        <h4 class="form-header h4" th:text="#{purchaseOrderDetails}">采购单明细信息</h4>
        <div class="row">
            <div class="col-xs-12">
                <button type="button" class="btn btn-white btn-sm" onclick="selectProductToParentCallBack()"><i class="fa fa-plus"> 增加</i></button>
                <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-suggest-js" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "business/purchase"
    $("#form-purchase-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-purchase-add').serialize());
        }
    }

    $("input[name='purchaseDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='expectedArrivalDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $(function() {
        var options = {
            pagination: false,
            search:true,
            showSearch:true,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    align: 'center',
                    title: i18n('serialNumber'),
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'productId',
                    align: 'center',
                    title:  i18n('productName'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtPurchaseOrderItemList[%s].productId' value='%s'>", index, value);
                            html += $.common.sprintf("<input class='form-control' type='hidden' name='jtPurchaseOrderItemList[%s].productName' value='%s'>", index, row.productName);
                        return html + row.productName;
                    }
                },
                {
                    field: 'productCode',
                    align: 'center',
                    title: i18n('sku'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtPurchaseOrderItemList[%s].productCode' value='%s'>", index, value);
                        return html + value;
                    }

                },
                {
                    field: 'styleNo',
                    align: 'center',
                    title: i18n('spu'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtPurchaseOrderItemList[%s].styleNo' value='%s'>", index, value);
                        return html + value;
                    }
                },
                {
                    field: 'spec',
                    align: 'center',
                    title: i18n('spec'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtPurchaseOrderItemList[%s].spec' value='%s'>", index, value);
                        return html + value;
                    }
                },
                {
                    field: 'unitPrice',
                    align: 'center',
                    title: i18n('purchaseUnitPrice'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' name='jtPurchaseOrderItemList[%s].unitPrice' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'qty',
                    align: 'center',
                    title: i18n('purchaseQuantity'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='number' name='jtPurchaseOrderItemList[%s].qty' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'amount',
                    align: 'center',
                    title: i18n('purchaseAmount'),
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='hidden' name='jtPurchaseOrderItemList[%s].amount' value='%s'>", index, value);
                        return html+ value;
                    }
                },
                {
                    title: i18n('operate'),
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
        };
        $.table.init(options);
    });

    function addRow(productInfo) {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;

        var purchasePrice = "";
            if(productInfo.purchasePrice != null){
                purchasePrice = productInfo.purchasePrice;
            } else {
                purchasePrice = productInfo.cost;
            }

        var purchaseQty = productInfo.quantity;
            if(purchaseQty == null){
                purchaseQty = 1;
            }
        var subAmount = purchaseQty * purchasePrice;
        var row = {
            index: $.table.serialNumber(count),
            productId: productInfo.productId,
            productName: productInfo.productName,
            productCode: productInfo.productCode,
            styleNo: productInfo.styleNo,
            spec: productInfo.spec,
            unitPrice: purchasePrice,
            qty: purchaseQty,
            amount: subAmount,
        }
        sub.addRow(row);
    }

    var productPrefix = ctx + "business/product";
    // callBack获取父窗口方法
    function selectProductToParentCallBack(){
        var options = {
            title: i18n('selectProduct'),
            url: productPrefix + "/purchase/select",
            callBack: doSubmit2
        };
        $.modal.openOptions(options);
    }

    function doSubmit2(index, layero){
        var selectedRow = layero.find("iframe")[0].contentWindow.getSelections();
        if (selectedRow.length == 0) {
            $.modal.alertWarning(i18n('selectOneProduct'));
            return;
        }

        selectedRow.forEach(function(row) {
            addRow(row);
        });

        $.modal.close(index);
    }
</script>

<script th:inline="javascript">
    var warehouseSuggest = $("#suggest-warehouse").bsSuggest({
        url: ctx + "business/warehouse/select/list",
        idField: "warehouseId",
        keyField: "warehouseName",
        clearable: true,
        ignorecase: true,
        hideOnSelect: true,
        effectiveFields: ["warehouseId", "warehouseName"],
        effectiveFieldsAlias: {warehouseId: "仓库ID", warehouseName: "仓库"},
    }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
        console.log('onSetSelectValue: ', selectedData);

        $("input[name='warehouseId']").val(selectedRawData.warehouseId);
    }).on('onUnsetSelectValue', function (e) {
        //console.log("onUnsetSelectValue=="+e);
        $("input[name='warehouseId']").val("");
    });


    var supplierSuggest = $("#suggest-supplier").bsSuggest({
        url: ctx + "business/supplier/select/list",
        idField: "supplierId",
        keyField: "supplierName",
        clearable: true,
        ignorecase: true,
        hideOnSelect: true,
        effectiveFields: ["supplierId", "supplierName","supplierCode"],
        effectiveFieldsAlias: {supplierId: "供应商ID", supplierName: "供应商名称", supplierCode:'供应商编码'},
    }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
        console.log('onSetSelectValue: ', selectedData);
        $("input[name='supplierId']").val(selectedRawData.supplierId);
    }).on('onUnsetSelectValue', function (e) {
        //console.log("onUnsetSelectValue=="+e);
        $("input[name='supplierId']").val("");
    });
</script>

<script th:inline="javascript">
    var ROOT = [[${#servletContext.contextPath}]];
    var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]];

    //初始化i18n
    $.i18n.properties({
        path: ROOT + '/i18n/',//这里表示访问路径
        name: 'messages',//文件名开头
        language: LANG_COUNTRY,//文件名语言 例如en_US
        mode: 'map'//默认值
    });

    //i18n函数
    function i18n(msgKey) {
        try {
            return $.i18n.prop(msgKey);
        } catch (e) {
            return msgKey;
        }
    }
</script>
</body>
</html>